﻿//**************************
//开发人员：Morning Sun QQ:576830428  微信:MorningSun0125
//业务范围：网站开发、小程序开发、web应用开发,桌面程序开发,工控类软件开发等
//开发日期：2016年1月10日
//版本号：Version 3.0 CopyRight 2011-2029
//**************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace FileServer
{
    #region  使用说明文件
    /*页面输入验证类使用说明
     *页面元素必须指定如下属性
     *BlankText：该元素消息提示框的标题内容
     *AllowBlank：该元素是否必填，true必填，false不为必填
     *MinLengthText：输入字符最小长度（当输入类型不为小数或者数字时），输入字符最小值（当输入类型为小数或者数字时）
     *MaxLengthText：输入字符最大长度（当输入类型不为小数或者数字时），输入字符最大值（当输入类型为小数或者数字时）
     *ValidateOnBlur:设置为false
     *ValidateOnChange:设置为false
     *VtypeText：输入值的数据类型，具体类型如下：
     *dhint:数字类型（可带正负号）
     *dhfloat：小数类型（可带正负号）
     *dhstring：任意字符串类型
     *dhenglish：英文字符串
     *dhchinese：中文字符串
     *dhtel：手机号码
     *dhphone：电话号码
     *dhcardid：身份证号码
     *dhpostcode：邮政编码
     *dhqq：qq号码
     *dhip：ip地址
     *dhmail：邮箱地址
     *dhurl：网址
     *dhfax:传真
     *dhnum:数字
     *页面中调用方式为：PageValidate(inputV, [控件ID].AllowBlank, [控件ID].Vtype, [控件ID].MinLength, [控件ID].MaxLength);
     */
    #endregion

    public  class FormValidate
    {
        #region  验证变量
        private static Regex RegNumber = new Regex("^[0-9]+$");
        private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
        private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
        private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
        private static Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串，和 [a-zA-Z0-9] 语法一样 
        private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
        private static Regex RegPhone = new Regex(@"^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$");//电话号码
        private static Regex RegTel = new Regex(@"^[1]+[3,5,8,7]+\d{9}");//手机号码
        private static Regex RegIDCard = new Regex(@"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");//身份证号码
        private static Regex RegPostCode = new Regex(@"^\d{6}$");//邮政编码
        private static Regex RegIP = new Regex(@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");//ip地址
        private static Regex RegQQ = new Regex(@"^[1-9][0-9]{4,9}$");//QQ号码
        private static Regex RegUrl = new Regex(@"^(?<![\w@]+)((http|https)://)?(www.)?[a-z0-9\.]+(\.(com|net|cn|com\.cn|com\.net|net\.cn))(/[^\s\n]*)?$");//网址
        private static Regex RegFax = new Regex(@"^((\+?[0-9]{2,4}\-[0-9]{3,4}\-)|([0-9]{3,4}\-))?([0-9]{7,8})(\-[0-9]+)?$");
        private static Regex RegNum = new Regex(@"^[0-9]*$");
        private static Regex RegDirPath = new Regex(@"^[[A-Za-z]:[^\?\/\*\|<>]+$");
        #endregion
      

        #region 判断是否为空
        /// <summary>
        /// 输入字符串
        /// </summary>
        /// <param name="InputString"></param>
        /// <returns></returns>
        public static bool IsEmpt(string InputString)
        {
            if (string.IsNullOrEmpty(InputString) || InputString.Length==0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion


        #region 判断一个数是否在一个闭区间
        /// <summary>
        /// 判断一个数是否在一个闭区间
        /// </summary>
        /// <param name="valNum">需要验证的值</param>
        /// <param name="mMin">最小值</param>
        /// <param name="mMax">最大值</param>
        public static bool IsInner(int valNum, int mMin, int mMax)
        {
           if(valNum>=mMin&&valNum<=mMax)
           {
             return true;
           }
           else
           {
             return false;
           }
        }


        /// <summary>
        /// 判断一个数是否在一个闭区间
        /// </summary>
        /// <param name="valNum">需要验证的值</param>
        /// <param name="mMin">最小值</param>
        /// <param name="mMax">最大值</param>
        public static bool IsInner(Double valNum, Double mMin, Double mMax)
        {
            if (valNum >= mMin && valNum <= mMax)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion


        #region  判断两个数是否相等
        /// <summary>
        /// 判断两个数是否相等
        /// </summary>
        /// <param name="valNum">标准数</param>
        /// <param name="inputNum">输入数</param>
        /// <returns></returns>
        public static bool IsEqual(int valNum, int inputNum)
        {
            if (valNum == inputNum)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion


        #region 是否数字字符串
        /// <summary>
        /// 是否数字字符串
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumber(string inputData)
        {
            Match m = RegNumber.Match(inputData);
            return m.Success;
        }
        #endregion


        #region 是否是带符号的数字
        /// <summary>
        /// 是否数字字符串 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumberSign(string inputData)
        {
            Match m = RegNumberSign.Match(inputData);
            return m.Success;
        }
        #endregion


        #region  是否为浮点数
        /// <summary>
        /// 是否是浮点数
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimal(string inputData)
        {
            Match m = RegDecimal.Match(inputData);
            if (!m.Success)
            {
                return IsNumber(inputData);
            }
            else
            {
                return true;
            }
        }
        #endregion


        #region 是否为带符号的浮点数
        /// <summary>
        /// 是否是浮点数 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimalSign(string inputData)
        {
            Match m = RegDecimalSign.Match(inputData);
            if (!m.Success)
            {
                return IsNumberSign(inputData);
            }
            else
            {
                return true;
            }
        }
        #endregion


        #region  检测是否有中文字符
        /// <summary>
        /// 检测是否有中文字符
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsHasCHZN(string inputData)
        {
            Match m = RegCHZN.Match(inputData);
            return m.Success;
        }
        #endregion


        #region 邮件地址
        /// <summary>
        /// 邮件地址
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsEmail(string inputData)
        {
            Match m = RegEmail.Match(inputData);
            return m.Success;
        }

        #endregion


        #region 电话号码
        /// <summary>
        /// 电话号码
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsPhone(string inputData)
        {
            Match m = RegPhone.Match(inputData);
            return m.Success;
        }

        #endregion


        #region 手机号码
        /// <summary>
        /// 手机号码
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsTel(string inputData)
        {
            Match m =  RegTel.Match(inputData);
            return m.Success;
        }

        #endregion


        #region 身份证号码
        /// <summary>
        /// 身份证号码
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsCardID(string inputData)
        {
            Match m = RegIDCard.Match(inputData);
            return m.Success;
        }

        #endregion


        #region 邮政编码
        /// <summary>
        /// 邮政编码
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsPostCode(string inputData)
        {
            Match m = RegPostCode.Match(inputData);
            return m.Success;
        }

        #endregion


        #region ip地址
        /// <summary>
        /// ip地址
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsIP(string inputData)
        {
            Match m = RegIP.Match(inputData);
            return m.Success;
        }

        #endregion


        #region QQ号码
        /// <summary>
        /// QQ号码
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsQQ(string inputData)
        {
            Match m = RegQQ.Match(inputData);
            return m.Success;
        }

        #endregion


        #region 网址
        /// <summary>
        /// 网址
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsUrl(string inputData)
        {
            Match m = RegUrl.Match(inputData);
            return m.Success;
        }

        #endregion


        #region 传真
        /// <summary>
        /// 传真
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsFax(string inputData)
        {
            Match m = RegFax.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 文件夹路径
        /// <summary>
        /// 文件夹路径
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsDirPath(string inputData)
        {
            Match m = RegDirPath.Match(inputData);
            return m.Success;
        }
        #endregion

        #region 必须为数字字符串
        /// <summary>
        /// 必须为数字字符串
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsNum(string inputData)
        {
            Match m = RegNum.Match(inputData);
            return m.Success;
        }
        #endregion
        

        #region   数据验证
        /// <summary>
        /// 数据验证
        /// </summary>
        /// <param name="InputValue">输入值</param>
        /// <param name="IsMustInput">是否为必须验证</param>
        /// <param name="InputType">输入类型</param>
        /// <param name="MinLen">最小长度</param>
        /// <param name="MaxLen">最大长度</param>
        /// <param name="MaxValue">最大值</param>
        /// <param name="MinValue">最小值</param>
        /// <returns></returns>
        public static string PageValidate(string InputValue, bool IsMustInput, string InType, int MinLen, int MaxLen)
        {
            string ResultMsg = "";
            if (IsMustInput)//必填项
            {
                if (FormValidate.IsEmpt(InputValue))
                {
                    ResultMsg = "此项为必填项!";
                }
                else
                {
                    ResultMsg = PageValidate(InputValue, InType, MinLen, MaxLen);
                }
            }
            else//不必填项
            {
                if (!FormValidate.IsEmpt(InputValue))//如果内容不为空需要验证
                {
                    ResultMsg = PageValidate(InputValue, InType, MinLen, MaxLen);
                }
            }
            return ResultMsg;
        }


        /// <summary>
        /// 验证方法
        /// </summary>
        /// <param name="InputValue">输入值</param>
        /// <param name="InType">数据类型</param>
        /// <param name="MinLen">最小值</param>
        /// <param name="MaxLen">最大值</param>
        /// <returns></returns>
        private static string PageValidate(string InputValue, string InType, int MinLen, int MaxLen)
        {
            string ResultMsg = "";

            #region 根据数据类型验证
            switch (InType.ToLower().Trim())
            {
                case "dhint":
                    #region 数字类型
                    if (!FormValidate.IsNumberSign(InputValue))
                    {
                        ResultMsg = "此项必须输入数字";
                    }
                    else
                    {
                        if (MinLen != 0 || MaxLen != 0)
                        {
                            if (!FormValidate.IsInner(float.Parse(InputValue), MinLen, MaxLen))
                            {
                                ResultMsg = "此项输入值必须大于等于" + MinLen + ",小于等于" + MaxLen + "!";
                            }
                        }
                    }
                    #endregion
                    break;
                case "dhfloat":
                    #region 小数类型
                    if (!FormValidate.IsDecimalSign(InputValue))
                    {
                        ResultMsg = "此项必须输入小数";
                    }
                    else
                    {
                        if (MinLen != 0 || MaxLen != 0)
                        {
                            if (!FormValidate.IsInner(Double.Parse(InputValue), MinLen, MaxLen))
                            {
                                ResultMsg = "此项输入值必须大于等于" + MinLen + ",小于等于" + MaxLen + "!";
                            }
                        }
                    }
                    #endregion
                    break;
                case "dhstring":
                    #region  字符串类型
                    if (MaxLen != 0 || MinLen != 0)
                    {
                        if (MaxLen == MinLen)
                        {
                            if (!FormValidate.IsEqual(MaxLen, InputValue.Trim().Length))
                            {
                                ResultMsg = "此项输入字符长度必须等于" + MinLen + "字符!";
                            }
                        }
                        else
                        {
                            if (!FormValidate.IsInner(InputValue.Trim().Length, MinLen, MaxLen))
                            {
                                ResultMsg = "此项输入字符长度必须大于等于" + MinLen + "字符，小于等于" + MaxLen + "字符!";
                            }
                        }
                    }
                    #endregion
                    break;
                case "dhenglish":
                    #region 必须是英文字符
          
                        if (FormValidate.IsHasCHZN(InputValue))
                        {
                            ResultMsg = "此项必须输入英文字符!";
                        }
                        else
                        {
                            if (MaxLen != 0 || MinLen != 0)
                            {
                                if (MaxLen == MinLen)
                                {
                                    if (!FormValidate.IsEqual(MaxLen, InputValue.Trim().Length))
                                    {
                                        ResultMsg = "此项输入字符长度必须等于" + MinLen + "字符!";
                                    }
                                    else
                                    {
                                        if (FormValidate.IsHasCHZN(InputValue))
                                        {
                                            ResultMsg = "此项必须输入英文字符!";
                                        }
                                    }
                                }
                                else
                                {
                                    if (!FormValidate.IsInner(InputValue.Trim().Length, MinLen, MaxLen))
                                    {
                                        ResultMsg = "此项输入字符长度必须大于等于" + MinLen + "字符，小于等于" + MaxLen + "字符!";
                                    }
                                    else
                                    {
                                        if (FormValidate.IsHasCHZN(InputValue))
                                        {
                                            ResultMsg = "此项必须输入英文字符!";
                                        }
                                    }
                                }
                            }
                        }
                   
                    #endregion
                    break;
                case "dhchinese":
                    #region 必须是中文字符
                    if (MaxLen != 0 || MinLen != 0)
                    {
                        if (MaxLen == MinLen)
                        {
                            if (!FormValidate.IsEqual(MaxLen, InputValue.Trim().Length))
                            {
                                ResultMsg = "此项输入字符长度必须等于" + MinLen + "字符!";
                            }
                            else
                            {
                                if (!FormValidate.IsHasCHZN(InputValue))
                                {
                                    ResultMsg = "此项必须输入中文字符!";
                                }
                            }
                        }
                        else
                        {
                            if (!FormValidate.IsInner(InputValue.Trim().Length, MinLen, MaxLen))
                            {
                                ResultMsg = "此项输入字符长度必须大于等于" + MinLen + "字符，小于等于" + MaxLen + "字符!";
                            }
                            else
                            {
                                if (!FormValidate.IsHasCHZN(InputValue))
                                {
                                    ResultMsg = "此项必须输入中文字符!";
                                }
                            }
                        }
                    }
                    else
                    {
                        if (!FormValidate.IsHasCHZN(InputValue))
                        {
                            ResultMsg = "此项必须输入中文字符!";
                        }
                    }
                    #endregion
                    break;
                case "dhtel":
                    #region 必须为手机号码
                    if (!FormValidate.IsEqual(MinLen, InputValue.Trim().Length))
                    {
                        ResultMsg = "此项输入字符长度必须等于" + MinLen + "字符!";
                    }
                    else
                    {
                        if (!FormValidate.IsTel(InputValue.Trim()))
                        {
                            ResultMsg = "此项必须输入手机号码!";
                        }
                    }
                    #endregion
                    break;
                case "dhphone":
                    #region  电话号码
                    if (!FormValidate.IsPhone(InputValue.Trim()))
                    {
                        ResultMsg = "此项必须输入电话号码!";
                    }
                    #endregion
                    break;
                case "dhcardid":
                    #region  身份证号码
                    if (FormValidate.IsEqual(MinLen, InputValue.Trim().Length) || FormValidate.IsEqual(MaxLen, InputValue.Trim().Length))
                    {
                        if (!FormValidate.IsCardID(InputValue.Trim()))
                        {
                            ResultMsg = "此项必须输入身份证号码!";
                        }
                    }
                    else
                    {
                        ResultMsg = "此项必须输入值长度必须为" + MinLen + "位或者为" + MaxLen + "位!";
                    }
                    #endregion
                    break;
                case "dhpostcode":
                    #region  邮政编码
                    if (!FormValidate.IsPostCode(InputValue))
                    {
                        ResultMsg = "此项必须输入邮政编码!";
                    }
                    #endregion
                    break;
                case "dhqq":
                    #region  QQ号码
                    if (!FormValidate.IsQQ(InputValue))
                    {
                        ResultMsg = "此项必须输入QQ号码!";
                    }
                    #endregion
                    break;
                case "dhip":
                    #region ip地址
                    if (!FormValidate.IsIP(InputValue))
                    {
                        ResultMsg = "此项必须输入IP地址!";
                    }
                    #endregion
                    break;
                case "dhmail":
                    #region 必须为邮箱
                    if (!FormValidate.IsEmail(InputValue.Trim()))
                    {
                        ResultMsg = "此项必须输入邮箱地址!";
                    }
                    #endregion
                    break;
                case "dhurl":
                    #region 必须为网址
                    if (!FormValidate.IsUrl(InputValue.Trim()))
                    {
                        ResultMsg = "此项必须输入网址!";
                    }
                    #endregion
                    break;
                case "dhfax":
                    #region  必须为传真
                    if (!FormValidate.IsFax(InputValue.Trim()))
                    {
                        ResultMsg = "此项必须输入传真号!";
                    }
                    #endregion
                    break;
                case  "dhnum":
                    #region  必须为数字字符串
                    if (!FormValidate.IsNum(InputValue.Trim()))
                    {
                        ResultMsg = "此项必须输入数字!";
                    }
                    else
                    {
                        if (MinLen != 0 || MaxLen != 0)
                        {
                            if (!FormValidate.IsInner(InputValue.Length, MinLen, MaxLen))
                            {
                                ResultMsg = "此项输入值长度必须大于等于" + MinLen + ",小于等于" + MaxLen + "!";
                            }
                        }
                    }
                    #endregion
                    break;
            }
            #endregion

            return ResultMsg;
        }
        #endregion


        #region 验证输入字符串是否与模式字符串匹配
        /// <summary>
        /// 验证输入字符串是否与模式字符串匹配，匹配返回true
        /// </summary>
        /// <param name="input">输入字符串</param>
        /// <param name="pattern">模式字符串</param>        
        public static bool IsMatch(string input, string pattern)
        {
            return IsMatch(input, pattern, RegexOptions.IgnoreCase);
        }


        /// <summary>
        /// 验证输入字符串是否与模式字符串匹配，匹配返回true
        /// </summary>
        /// <param name="input">输入的字符串</param>
        /// <param name="pattern">模式字符串</param>
        /// <param name="options">筛选条件</param>
        public static bool IsMatch(string input, string pattern, RegexOptions options)
        {
            return Regex.IsMatch(input, pattern, options);
        }
        #endregion

    }
}
